var Utils = {
    IsNullOrEmpty:function(str){
        if(typeof(str)=="undefined") return true;
        if(str==null) return true;
        if(str=="") return true;
        return false;
    },//IsNullOrEmpty
    GetRadioValue:function(radioName)
    {
        var radios = document.getElementsByName(radioName);
        for(var i = 0 ; i< radios.length; i++)
        {
            var radio = radios.item(i);
            if(radio.checked)
            {
                if(Utils.IsNullOrEmpty(radio.value)) return undefined;
                return radio.value;
            }
        }
        return undefined;
    },//GetRadioValue
    SetRadioValue:function(radioName,val)
    {
        var radios = document.getElementsByName(radioName);
        for(var i = 0 ; i< radios.length; i++)
        {
            var radio = radios.item(i);
            if(radio.value == val)
            {
                 radio.checked = true
            }
        }
    },//SetRadioValue
    GetCheckboxValue:function(checkboxName)
    {
        var checkboxs = document.getElementsByName(checkboxName);
        var ck = new Array();
        var j  =  0;
        for(var i = 0 ; i< checkboxs.length; i++)
        {
            var checkbox = checkboxs[i];
            if(checkbox.checked)
            {
                if(Utils.IsNullOrEmpty(checkbox.value)) return undefined;
                ck[j] = checkbox.value;
                j++;
            }
        }
        return j == 0 ? undefined : ck.join();
    },//GetCheckboxValue
    GetSelectValue:function(selectId)
    {
        var select = document.getElementById(selectId);
        var options = select.options;
        for(var i = 0 ; i<options.length ;i++)
        {
            var option = options[i];
            if(option.selected)
            {
                if(Utils.IsNullOrEmpty(option.value)) return undefined;
                return option.value;
            }
        }
        return undefined;
    },//GetSelectValue
    SetSelectValue:function(selectId,val)
    {
        var select = document.getElementById(selectId);
        var options = select.options;
        for(var i = 0 ; i<options.length ;i++)
        {
            var option = options[i];
            if(option.value == val)
            {
                option.selected = true
            }
        }
    },//SetSelectValue
    SetRadioStatus:function(select,val)
    {
        if(val == 0){
            for(var i = 0 ; i<select.length ;i++)
            {
                select[i].disabled = true;
            }
        }else{
            for(var i = 0 ; i<select.length ;i++)
            {
                select[i].disabled = false;
            }
        }
    },//SetRadioStatus
    SetInputStatus:function(select,val)
    {
        if(val == 0){
            for(var i = 0 ; i<select.length ;i++)
            {
                select[i].disabled = true
            }
        }else{
            for(var i = 0 ; i<select.length ;i++)
            {
                select[i].disabled = false;
            }
        }
    },//SetInputStatus
}

var baseUrl = './'
// if(window.location.host == '192.168.137.1:63342') {
//     baseUrl = ''
//
// }

var Ajax = {
    get: function(url, fn) {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
          fn.call(this, xhr.responseText);
        }
      };
      xhr.send();
    },
    post: function (url, data, fn) {
      var xhr = new XMLHttpRequest();
      xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
          fn.call(this, xhr.responseText);
        }
      };
      xhr.send(data);
    }
}

var Message = {
    info: function(info) {
        var html = document.createElement('div')
        html.className = 'mask'
        html.innerHTML = `<div class="mask-content">
                                <p class="mask-info">${info}</p>
                                <div class="mask-close">OK</div>
                            </div>`
        document.body.appendChild(html)
        document.getElementsByClassName("mask-close")[0].onclick=function(){
            var dn = document.getElementsByClassName("mask")[0]
            dn.parentNode.removeChild(dn);
        }
      },
      showmsg(msg) {
        var c = document.getElementsByClassName("showmsg")
        if (c.length > 0) {
            document.getElementsByClassName("showmsg")[0].children[0].innerText = msg
            document.getElementsByClassName("showmsg")[0].style.display = 'block'
        } else {
        var htm = document.createElement('div')
        htm.className = 'showmsg'
        htm.innerHTML = `<span>${msg}</span>`
        document.body.appendChild(htm)
        document.getElementsByClassName("showmsg")[0].style.display = 'block'
        }
        setTimeout(function() {
            document.getElementsByClassName("showmsg")[0].style.display = 'none'
        }, 3000);
    }
}

function toPage(page) {
    window.location.href = page
}

function IPTest(ip) {
    var reg = /^(?!^255(\.255){3}|^127)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)){2}\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/
    return reg.test(ip);
}

function IPV6Test(ipv6) {
    var reg = /^([\da-fA-F]{1,4}(:[\da-fA-F]{1,4}){7}|[\da-fA-F]{1,4}(:[\da-fA-F]{1,4}){0,7}::[\da-fA-F]{0,4}(:[\da-fA-F]{1,4}){0,7}|(:[\da-fA-F]{1,4}){0,7}::[\da-fA-F]{0,4}(:[\da-fA-F]{1,4}){0,7})$/
    return reg.test(ipv6);
}

function markTest(mark) {
    var exp =  /^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/; 
    return exp.test(mark);
}

function portTest(port) {
    var exp=/^(\d)+$/g;
    if(exp.test(port) && parseInt(port)<=65535 && parseInt(port)>=0){
     return true;
    }
    return false;
}

function macTest(mac) {
    var a=mac.toLowerCase()
    if(a == 'ff:ff:ff:ff:ff:ff'){
        return false
    }else{
        var reg = /^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$/
        return reg.test(mac);
    }
    
}

function dnsTest(dns) {
    var reg = /^(?!^255(\.255){3}|^127)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)){2}\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/
    return reg.test(dns);
}


function getBytes(str){
    var data = {
        bytesCount: 0,
        chineseStatus: false
    }
    for (var i = 0; i < str.length; i++)
    {
        var c = str.charAt(i);
        if (/^[\u0000-\u00ff]$/.test(c)) // Match the double byte.
        {
            data.bytesCount += 1;
        }
        else
        {
            data.chineseStatus = true;
            data.bytesCount += 2;
        }
    }
    return data
}

function testAction() {
　　var lastTime = new Date().getTime();
　　var currentTime = new Date().getTime();
　　var timeOut = 10 * 60 * 1000; //timeout: 10min

    function testTime(){
    　currentTime = new Date().getTime(); //update current time
    　if(currentTime - lastTime > timeOut){ //is timeout?
    　　window.location.href = "/login.html";
    　}
    }
    document.onmousemove = function() {
        lastTime = new Date().getTime(); //update current time
    }
    /* timer to check per 5s */
    window.setInterval(testTime, 5000);
}



